﻿
<!DOCTYPE html>
<html lang="zh-cn" class="fullscreen-bg" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head th:replace="common/links :: common_header(~{::title},~{},~{})">
    <title>流程定义管理</title>
</head>
<body>
<!-- 页面加载loading -->
<div class="page-loading">
    <div class="ball-loader">
        <span></span><span></span><span></span><span></span>
    </div>
</div>
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 表格工具栏 -->
            <form class="layui-form toolbar">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">流程名称:</label>
                        <div class="layui-input-inline">
                            <input name="name" class="layui-input" placeholder="流程名称"/>
                        </div>
                    </div>
                    <div class="layui-inline">&emsp;
                        <button class="layui-btn icon-btn" lay-filter="searchBtn" lay-submit>
                            <i class="layui-icon">&#xe615;</i>搜索
                        </button>&nbsp;
                        <button shiro:hasPermission="flow_procdef:export" id="exportBtn" class="layui-btn icon-btn" type="button">
                            <i class="layui-icon">&#xe67d;</i>导出
                        </button>
                    </div>
                </div>
            </form>
            <table id="dataTable" lay-filter="dataTable"></table>
        </div>
    </div>
</div>

<!-- 表单弹窗 -->
<script type="text/html" id="formDialog">
    <form id="dataForm" lay-filter="dataForm" class="layui-form model-form">
        <input name="id" type="hidden"/>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">模型名称:</label>
            <div class="layui-input-block">
                <input name="name" placeholder="请输入模型名称" class="layui-input"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">模型标识:</label>
            <div class="layui-input-block">
                <input name="key" placeholder="请输入模型标识" class="layui-input"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">备注信息</label>
            <div class="layui-input-block">
                <textarea name="description" placeholder="请输入备注信息" class="layui-textarea" maxlength="200"></textarea>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn" lay-filter="saveBtn" lay-submit>保存</button>
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
        </div>
    </form>
</script>

<!-- 表格操作列 -->
<script type="text/html" id="tableBar">
    {{# if (d.suspended) { }}  
    <a shiro:hasPermission="flow_procdef:activate" class="layui-btn layui-btn-warm layui-btn-xs" lay-event="activate">激活</a>
    {{# } else { }}  
    <a shiro:hasPermission="flow_procdef:suspend" class="layui-btn layui-btn layui-btn-xs" lay-event="suspend">挂起</a>
    {{# } }}
    <a shiro:hasPermission="flow_procdef:delete" class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<!-- 表格状态列 -->
<script type="text/html" id="suspended">
    <p>{{d.suspended?'<font color="red">挂起</font>':'<font color="green">激活</font>'}}</p>
</script>

<script type="text/html" id="diagramResourceName">
    <a lay-event="viewImg"><i class="layui-icon layui-icon-picture"></i></a>
</script>

<script type="text/html" id="resourceName">
    <a lay-event="viewBpmn"><i class="layui-icon layui-icon-template-1"></i></a>
</script>

<!-- js部分1 -->
<div th:replace="common/scripts :: jquery"></div>
<div th:replace="common/scripts :: common"></div>
<div th:replace="common/scripts :: custom"></div>
<script th:inline="javascript">
    layui.use(['layer', 'form', 'table', 'util', 'laydate', 'tableX', 'admin', 'index'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var util = layui.util;
        var laydate = layui.laydate;
        var tableX = layui.tableX;
        var admin = layui.admin;
        var index = layui.index;


        /* 渲染表格 */
        var cols = [
            {type: 'checkbox'},
            {field: 'id', title: '定义ID', width: 150},
            {field: 'category', title: '业务类型',  templet: function (d) {
                    if(d.category === "0"){
                        return "事项审批"
                    }else if(d.category === "1"){
                        return "人事流程"
                    }else if(d.category === "2"){
                        return "合同流程"
                    }else{
                        return "其他流程"
                    }}, align: 'center'},
            {field: 'name', title: '流程名称'},
            {field: 'key', title: '流程标识'},
            {field: 'version', title: '版本',align: 'center',templet: function (d) {
                    return "v"+d.version;
                }
            },
            {field: 'diagramResourceName', title: '流程图', templet: '#diagramResourceName', align: 'center'},
            {field: 'resourceName', title: '流程文件', templet: '#resourceName',align: 'center'},
            {field: 'suspended', title: '状态', templet: '#suspended', align: 'center'},
            {title: '操作', toolbar: '#tableBar', align: 'center', minWidth: 200}
        ];

        var insTb = table.render({
            elem: '#dataTable',
            url: 'procdef/page',
            page: true,
            cellMinWidth: 100,
            cols: [cols],
            done: function () {
                // 绑定鼠标右键
                // tableX.bindCtxMenu('dataTable', shiroBindCtxMenu(shiroDelete,shiroUpdate));
            }
        });

        /* 渲染时间选择 */
        laydate.render({
            elem: 'input[name="dateRange"]',
            type: 'date',
            range: true,
            trigger: 'click'
        });


        /* 表格工具条点击事件 */
        table.on('tool(dataTable)', function (obj) {
            if (obj.event === 'suspend') { // 挂起流程
                suspend(obj.data);
            } else if (obj.event === 'activate') { // 激活流程
                activate(obj.data);
            } else if (obj.event === 'exportXml') { // 导出模型
                resetPsw(obj.data);
            } else if (obj.event === 'del') { // 删除模型
                doDel(obj.data);
            } else if (obj.event === 'viewImg') { // 查看模型图
                previewImg('procdef/viewResource?type=img&deploymentId='+obj.data.deploymentId);
            } else if (obj.event === 'viewBpmn') { // 查看模型文件
                var url='procdef/viewResource?type=bpmn&deploymentId='+obj.data.deploymentId
                window.open(url, "流程文件", "toolbar=no, menubar=no, scrollbars=no, resizable=no,location=no, status=no");
            }
        });

        /* 表格头工具栏点击事件 */
        table.on('toolbar(dataTable)', function (obj) {
            if (obj.event === 'add') { // 添加
                showEditModel();
            } else if (obj.event === 'del') { // 删除
                var checkRows = table.checkStatus('dataTable');
                if (checkRows.data.length === 0) {
                    layer.msg('请选择要删除的数据', {icon: 2});
                    return;
                }
                var ids = checkRows.data.map(function (d) {
                    return d.id;
                });
                doDel({ids: ids});
            }
        });

        // 挂起流程
        function suspend(data) {
            $.post('procdef/suspend',{id:data.id}, function (res) {
                if (res.code == 0) {
                    layer.msg(res.msg, {icon: 1});
                    insTb.reload({page: {curr: 1}});
                } else {
                    layer.msg(res.msg, {icon: 2});
                }

            }, 'json');
        }
        // 启动流程
        function activate(data) {
            $.post('procdef/activate',{id:data.id}, function (res) {
                if (res.code == 0) {
                    layer.msg(res.msg, {icon: 1});
                    insTb.reload({page: {curr: 1}});
                } else {
                    layer.msg(res.msg, {icon: 2});
                }
            }, 'json');
        }

        // 显示编辑弹窗
        function showEditModel() {
            admin.open({
                type: 1,
                area: '800px',
                title: ('创建新模型'),
                content: $('#formDialog').html(),
                shadeClose:true,
                success: function (layero, dIndex) {
                    form.on('submit(saveBtn)', function (data) {
                        $.post('procdef/create', data.field, function (res) {
                            if (res.code == 0) {
                                layer.close(dIndex);
                                layer.msg(res.msg, {icon: 1});
                                insTb.reload({page: {curr: 1}});
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, 'json');
                        return false;
                    });
                }
            });
        }


        /* 表格搜索 */
        form.on('submit(searchBtn)', function (data) {
            if (data.field.dateRange) {
                var searchDate = data.field.dateRange.split(' - ');
                data.field.startDate = searchDate[0];
                data.field.endDate = searchDate[1];
            } else {
                data.field.startDate = '';
                data.field.endDate = '';
            }
            data.field.dateRange = undefined;
            insTb.reload({where: data.field, page: {curr: 1}});
            return false;
        });

        /* 删除 */
        window.doDel = function(obj){
            if ([[${session.session_runtime}]] == 'demo') return layer.msg('演示环境不允许操作，请联系管理员', {icon: 4});
            layer.confirm('确定要删除选中数据吗？', {
                skin: 'layui-layer-admin',
                shade: .1
            }, function (i) {
                layer.close(i);
                var loadIndex = layer.load(2);
                $.post('procdef/delete', {
                    id: obj ? obj.id : '',
                    ids: obj.ids ? obj.ids.join(',') : ''
                }, function (res) {
                    layer.close(loadIndex);
                    if (res.code === 0) {
                        layer.msg(res.msg, {icon: 1});
                        insTb.reload({page: {curr: 1}});
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'json');
            });
        }

        /* 导出excel */
        $('#exportBtn').click(function () {
            var checkRows = table.checkStatus('dataTable');
            if (checkRows.data.length === 0) {
                layer.msg('请选择要导出的数据', {icon: 2});
            } else {
                tableX.exportDataX({
                    cols: insTb.config.cols,
                    data: checkRows.data,
                    fileName: '导出数据'
                });
            }
        });
    });
</script>
</body>
</html>